﻿using System.Linq;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
namespace FacialClub.Models.Repositories
{
    /// <summary>
    /// Email Template Repository
    /// </summary>
    public class EmailTemplateRepository :  IEmailTemplateRepository
    {
        readonly FacialClubEntities _db = new FacialClubEntities();

        /// <summary>
        /// Get Template Details by Template Name
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public IQueryable<EmailTemplate> FindEmailTemplateByName(string q)
        {
            return _db.EmailTemplates.Where(d => d.EmailTemplateName.Contains(q));
        }

        /// <summary>
        /// Get All Templates
        /// </summary>
        /// <returns></returns>
        public IQueryable<EmailTemplate> FindAllEmailTemplate()
        {
            return _db.EmailTemplates;
        }

        /// <summary>
        /// Get Template by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public EmailTemplate GetEmailTemplate(int id)
        {
            return _db.EmailTemplates.SingleOrDefault(d => d.EmailTemplateId == id);
        }

        /// <summary>
        /// Add Template in the Database
        /// </summary>
        /// <param name="emailTemplate"></param>
        public void Add(EmailTemplate emailTemplate)
        {
            _db.EmailTemplates.AddObject(emailTemplate);
        }

        /// <summary>
        /// Delete Template from Database
        /// </summary>
        /// <param name="emailTemplate"></param>
        public void Delete(EmailTemplate emailTemplate)
        {
           _db.EmailTemplates.DeleteObject(emailTemplate);
        }
        //
        // Persistence 

        /// <summary>
        /// Save template in the database
        /// </summary>
        public void Save()
        {
            _db.SaveChanges();
        }
    }
}